package com.tingyuge.base.thread.tools;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * Created by xiaodao
 * date: 2020/3/23
 */
public class CountDownTest {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(4);
        IntStream.range(0,3).forEach(it->{
            new Thread(()->{
                try {
                    Thread.sleep(2000);
                    System.out.println("hello");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
            }).start();
        });
        System.out.println("启动子线程完毕");
        //如果不为0 将一直阻塞
        countDownLatch.await();
        System.out.println("主线程执行完毕");
    }
}
